package com.astar.exception;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.astar.dto.result.sys.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by liuruiyan on 2019/9/10.
 */
//全局异常处理，捕获所有Controller中抛出的异常
@ControllerAdvice
@Slf4j
public class GlobalControllerAdvice {


    /**
     * 自定义异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object exceptionHandler(Exception e){
        log.error(ExceptionUtil.stacktraceToString(e));
        e.printStackTrace();
        if(e instanceof SessionTokenNullException){
            return AjaxResult.error(3,"请求头token为空");
        }else{
            return AjaxResult.error(10,"系统异常");
        }
    }


    //    @ExceptionHandler(SessionTokenNullException.class)
//    public ModelAndView exceptionHandler(Exception ex){
//        ModelAndView mv = new ModelAndView("error");
//        mv.addObject("exception", ex);
//        System.out.println("in testControllerAdvice");
//        return mv;
//    }
}
